#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static char *prompt(char *line, int size)
{
    char *retval;

    do
    {
        printf(">> ");
        retval = fgets(line, size, stdin);
    } while (retval && *line == '\n');
    printf("retval:%s", retval);
    line[strlen(line) - 1] = '\0';
    return retval;
}

// send cmd
static void repl()
{
    int size = 1 << 12, max = size >> 1, argc, i;
    char buffer[size];
    char *line = buffer;
    char **ap, *args[max];
    while (prompt(line, size))
    {
        argc = 0;

        for (ap = args; (*ap = strsep(&line, " \t")) != NULL;)
        {
            if (**ap != '\0')
            {
                if (argc >= max)
                {
                    break;
                }
                if (strcasecmp(*ap, "quit") == 0 || strcasecmp(*ap, "exit") == 0)
                {
                    exit(0);
                }
                ap++;
                argc++;
            }
        }

        for (i = 0; i < argc; i++)
        {
            printf("param%d:%s\n", i, args[i]);
        }

        line = buffer;
    }

    exit(0);
}

int main(int argc, char const *argv[])
{
    repl(); //循环读取命令
    return 0;
}
